package day4;

import org.junit.Test;

import java.util.Scanner;

public class WorkerDemo {

    public static void main(String[] args) {
       /* Worker worker = new Worker();
        // worker.id = 4;
        // 实参：实际传递的参数
        worker.setId(1);
        worker.setName("小张");
        worker.setAge(23);
        worker.setAddress("济南");
        System.out.println(worker); // worker.toString()*/

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入工人的数量");
        int count = scanner.nextInt();
        Worker[] workers = new Worker[count];

        for (int i = 0; i < workers.length; i++) {
            System.out.println("请输入第" + (i + 1 ) + "个工人的id：");
            int id = scanner.nextInt();
            System.out.println("请输入第" + (i + 1 ) + "个工人的名字：");
            String name = scanner.next();
            System.out.println("请输入第" + (i + 1 ) + "个工人的年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入第" + (i + 1 ) + "个工人的地址：");
            String address = scanner.next();
            // 每次循环构造一个Worker工人对象
            Worker worker = new Worker();
            worker.setId(id);
            worker.setName(name);
            worker.setAge(age);
            worker.setAddress(address);
            // 把构造好的工人对象加到数组里面
            workers[i] = worker;
        }

        // 打印数组里面所有的工人信息
        for (int i = 0; i < workers.length; i++) {
            Worker worker = workers[i];
            System.out.println(worker);
        }

        while (true) {
            System.out.println("------------");
            System.out.println("输入1按照id搜索");
            System.out.println("输入2按照name搜索");
            System.out.println("输入3按照age搜索");
            System.out.println("输入4按照address搜索");
            System.out.println("输入0退出");
            System.out.println("------------");
            // 如果没有搜索到，打印一个“没有该信息的工人”
            int type = scanner.nextInt();
            if (type == 0) {
                System.out.println("退出程序");
                return;
            }

            boolean isSeachFind = false;
            switch (type) {
                case 1:// id搜索

                    break;
                case 2:// name搜索
                    System.out.println("请输入name：");
                    String name = scanner.next();
                    boolean isSearchByNameFind = false;
                    for (int i = 0; i < workers.length; i++) {
                        Worker worker = workers[i];
                        // 字符串比较是不是相等使用equals
                        if (name.equals(worker.getName())) {
                            System.out.println(worker);
                            isSearchByNameFind = true;
                        }
                    }
                    if (!isSearchByNameFind) {
                        System.out.println("没有改信息的工人");
                    }
                    /*if (isSearchByNameFind == false) {
                        System.out.println("没有改信息的工人");
                    }*/
                case 3:// age搜索
                    System.out.println("请输入年龄：");
                    int age = scanner.nextInt();
                    boolean isSearchByAgeFind = false;
                    for (int i = 0; i < workers.length; i++) {
                        Worker worker = workers[i];
                        if (age == worker.getAge()) {
                            System.out.println(worker);
                            isSearchByAgeFind = true;
                        }
                    }
                    if (!isSearchByAgeFind) {
                        System.out.println("没有该年龄学生");
                    }
                    break;
            }
        }

    }

    @Test
    public void test2() {
        Worker worker1 = new Worker();
        // worker.id = 4;
        // 实参：实际传递的参数
        worker1.setId(1);
        worker1.setName("小张");
        worker1.setAge(23);
        worker1.setAddress("济南");
        // day4.Worker@22927a81
        System.out.println(worker1); // worker.toString()

        Worker worker2 = new Worker(2, "李四", 23, "青岛");
        System.out.println(worker2);
    }
}
